﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ueEngine.Biz.Inf.LogicEntity;
using ueEngine.Biz.Core.ExServer.WorkPool.WorkHandle.FullSearch;
using ueEngine.Biz.Core.AppService.Recruitment;
using ueEngine.Framework.Common;

namespace ueEngine.Web.WebLogic.PageDataProxy
{
    public class PositionListQuery<T> : IDataProxy<PositionListQueryModel>
    {
        protected override bool CheckedParams(params string[] keys)
        {
            return true;
        }

        protected override string BuildKey(params string[] keys)
        {
            return CachePrefix + String.Format("PositionListQuery_{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}",
                keys[0], keys[1], keys[2], keys[3], keys[4], keys[5], keys[6], keys[7]);
        }

        protected override PositionListQueryModel BuildRefrashAction(params string[] keys)
        {
            string qs = keys[0];
            int qt = keys[1].ToInt32(0);
            int qj = keys[2].ToInt32(0);
            int areaCode = keys[3].ToInt32(0);
            int positionType = keys[4].ToInt32(0);
            string ag = keys[5];
            int page = keys[6].ToInt32(0);
            int pageSize = keys[7].ToInt32(50);
            PositionListQueryModel result = new PositionListQueryModel();
            if (String.IsNullOrEmpty(qs))
            {
                result.result = PositionSearchFromDB((RecruitSheetType)qt, (RecruitSheetSubject)qj, (AreaCode)areaCode, (PositionType)positionType, ag, page, pageSize);
            }
            else
            {
                result.result = new RecruitService(string.Empty).PositionSearch(qs, (RecruitSheetType)qt, (RecruitSheetSubject)qj, (AreaCode)areaCode, (PositionType)positionType, ag, page, pageSize);
            }
            return result;
        }
        private static ItemListBox<PositionCell> PositionSearchFromDB(RecruitSheetType rt, RecruitSheetSubject subject, AreaCode areacode, PositionType pt, string agent, int page, int pageSize)
        {
            var qr = new RecruitService(string.Empty).QueryPosition(page, pageSize,
                rt, subject, areacode, pt, agent);
            var result = new ItemListBox<PositionCell>()
            {
                Data = new List<PositionCell>(),
                CurrentPage = qr.CurrentPage,
                ItemTotle = qr.ItemTotle,
                NextPage = qr.NextPage,
                PrePage = qr.PrePage,
                TotlePage = qr.TotlePage,
                PageItemSegment = qr.PageItemSegment
            };
            int itemCount = 0;
            foreach (var p in qr.Data)
            {
                result.Data.Add(new PositionCell()
                {
                    Abstract = p.RecruitSheet.Caption,
                    WorkAddress = p.RecruitSheet.WorkAddress,
                    WorkArea = p.RecruitSheet.WorkArea.Mapping(),
                    PositionDescript = p.PositionDescript,
                    PositionId = p.Id,
                    CertificationLevel = (int)p.CertificationLevel,
                    PositionName = p.PositionName,
                    PositionType = (int)p.PositionType,
                    Priority = (int)p.Priority,
                    PublishDate = p.RecruitSheet.Refresh,//p.RecruitSheet.PublishDate,
                    //RecuritDescript = p.RecruitSheet.Description,
                    RecuritId = p.RecruitSheet.Id,
                    Title = p.RecruitSheet.Caption,
                    Url = "/home/jobdetail_" + p.RecruitSheet.Id,
                    CompanyName = p.DepositCompany.CompanyName,
                    RecruitSheetSubject = (int)p.RecruitSheet.RecruitSubject
                });
                itemCount++;

            }
            //result = result.BuildPage(itemCount, page, pageSize);
            return result;
        }

    }

    public class PositionListQueryModel
    {
        public ItemListBox<PositionCell> result;
    }
}
